import java.util.Calendar;


public class ServicedJob implements IServicedJob
{
	private IJob job = null;
	
	private int start_hour;
	private int start_minute;
	private int start_second;
	
	private int finish_hour = 0;
	private int finish_minute = 0;
	private int finish_second = 0;
	
	private Double powerConsumed = null;
	
	public ServicedJob(IJob job, Calendar startInstance, Double pC)
	{
		this.job = job;
		this.start_hour = startInstance.get(Calendar.HOUR_OF_DAY);
		this.start_minute = startInstance.get(Calendar.MINUTE);
		this.start_second = startInstance.get(Calendar.SECOND);
		this.powerConsumed = pC;
		
		calculateFinishTime();
		
		System.out.println("New serviced job created with sH: " + start_hour + " sM: " + start_minute + " sS: " + start_second);
		System.out.println("fH: " + finish_hour + " fM: " + finish_minute + " fS: " + finish_second);
	}
	
	private void calculateFinishTime()
	{
		int givenSeconds = job.getDuration();
		
		int extraSeconds = (int) (givenSeconds % 60);
		int extraMinutes = (int) ( (int) (givenSeconds/60) % 60);
		int extraHours = (int) (givenSeconds/3600);
		
		finish_hour = start_hour + extraHours;
		finish_minute = start_minute + extraMinutes;
		finish_second = start_second + extraSeconds;
		
		if(finish_second >= 60)
		{
			finish_second = finish_second - 60;
			finish_minute++;
		}
		
		if(finish_minute >= 60)
		{
			finish_minute = finish_minute - 60;
			finish_hour++;
		}
		
		if(finish_hour >= 24)
			finish_hour = finish_hour - 24;
	}
	
	public IJob getJob()
	{
		return job;
	}
	
	public Double getPowerConsumed()
	{
		return powerConsumed;
	}
	
	public boolean isJobOver(Calendar currentInstance)
	{		
		if((currentInstance.get(Calendar.HOUR_OF_DAY) >= finish_hour) 
				&& (currentInstance.get(Calendar.MINUTE) >= finish_minute) 
				&& (currentInstance.get(Calendar.SECOND) >= finish_second))
		{
			return true;
		}
			
		return false;
	}
	
	public int getEndHour()
	{
		return finish_hour;
	}
	
	public int getEndMinute()
	{
		return finish_minute;
	}
	
	public int getEndSecond()
	{
		return finish_second;
	}
}
